<template>
  <!-- 设备台账 -->
  <Layout>
      <Form ref="form1" :model="form1" :label-width="120">
        <Row class="query-form" style="padding-top: 15px">
            <Col span="8">
                <FormItem label="规格型号：">
                    <Input class="query-form-el" type="text" v-model="specModels"></Input>
                </FormItem>
            </Col>
            <Col span="8" offset="8">
                <Button @click="reset" class="ivu-btn-reset"  >重置</Button>
                <Button type="primary" class="ivu-btn-query"   icon="md-search"  @click="querySpecModels">查询</Button>
            </Col>
        </Row>
        <Row  v-show="switchFlag" class="query-form" style="padding-top: 8px">
            <Col span="8">
                <FormItem label="规格型号：">
                    <Input class="query-form-el" type="text" v-model="specModels"></Input>
                </FormItem>
            </Col>
        </Row>
        <Row v-show="switchFlag" class="query-form">
            <Col span="24">
                <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text"  >收起∧</Button>
                <Button @click="reset" class="ivu-btn-reset"   >重置</Button>
                <Button type="primary" class="ivu-btn-query"   icon="md-search" @click="querySpecModels">查询</Button>
            </Col>
        </Row>
        <Row class="query-form">
            <Col span="24">
                <Button size="large"  class="ivu-btn-add" icon="md-add"  @click="addSpecModels">新增</Button>
                <!--<Button size="large"  class="ivu-btn-delete" icon="md-trash" @click="batDelete">删除</Button>-->
            </Col>
        </Row>
      </Form>
      <!-- 响应结果内容展示 -->
      <Table ref='table' :height="tableHeight" size="small" :loading="loading" :row-class-name="rowClassName"  @on-selection-change = 'getSelectRows' no-data-text="没有查询到相关数据!"
           stripe border :columns="columns1" :data="resultData">
      </Table>
      <!-- 底部分页条 -->
    <Row style="margin-right:10px;">
        <Col span="24" >
                <Page style="float: right" 
                :total="dataCount" 
                :page-size="pageSize" 
                @on-change="changepage"
                 @on-page-size-change="nowPageSize" 
                 :page-size-opts="pageSizeArray" show-total show-sizer show-elevator />
        </Col>
    </Row>

    <Modal  title="规格型号" :transfer=false  v-model="model1"  :width=600 >
        <Row>
            <Col span="24">
                <Form ref="formCustom" :model="formCustom" :rules="ruleValidate"  label-position="top">
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="规格型号名称" prop="specModels">
                                <Input type="text" v-model="formCustom.specModels"></Input>
                            </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="规格型号编码" prop="specCode">
                                <Input type="text" v-model="formCustom.specCode"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="设备种类" prop="categoryId">
                                <Select v-model="formCustom.categoryId" >
                                    <Option v-for="item in categoryIds" :value="item.categoryId" :key="item.categoryId">{{ item.categoryName }}</Option>
                                </Select>
                            </FormItem>
                        </Col>
                        <Col span="11">
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23">
                            <FormItem label="备注" prop="notes">
                                <Input type="textarea" v-model="formCustom.notes"></Input>
                            </FormItem>
                        </Col>
                    </Row>
                   <!-- <Row type="flex" justify="center" class="code-row-bg">
                        <Col span="23" >
                            <FormItem>
                                <Button type="primary"  class='ivu-btn-save' :loading="buttonLoading" @click="submit('form1datacopy')" >确定</Button>
                                <Button type="primary"  class='ivu-btn-cancel' @click="handleCancel" >取消</Button>
                            </FormItem>
                        </Col>
                    </Row>-->
                </Form>
            </Col>
        </Row>
        <div slot="footer">
            <Button type="primary"  class='ivu-btn-save' :loading="buttonLoading" @click="submit('form1datacopy')" >确定</Button>
            <Button type="primary"  class='ivu-btn-cancel' @click="handleCancel" >取消</Button>
        </div>
    </Modal>

    <Modal title="规格型号" class="modal-view-detail"  :transfer=false v-model="model2"  :width=800>
        <Row>
            <Col span="24">
                <Form  :model="showdata" :label-width="160" >
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="规格型号名称" prop="specModels">
                                {{showdata.specModels}}
                            </FormItem>
                        </Col>
                        <Col span="11">
                            <FormItem label="规格型号编码" prop="specCode">
                                {{showdata.specCode}}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="11">
                            <FormItem label="设备种类" prop="categoryId">
                                {{showdata.categoryName}}
                            </FormItem>
                        </Col>
                        <Col span="11">
                        </Col>
                    </Row>
                    <Row type="flex" justify="space-around" class="code-row-bg">
                        <Col span="23">
                            <FormItem label="备注" prop="notes">
                                {{showdata.notes}}
                            </FormItem>
                        </Col>
                    </Row>
                   <!-- <Row>
                        <Col span="23" >
                            <FormItem>
                                <Button class='ivu-btn-save' @click="model2=false">关闭</Button>
                            </FormItem>
                        </Col>
                    </Row>-->
                </Form>
            </Col>
        </Row>
        <div slot="footer">
            <Button class='ivu-btn-save' @click="model2=false">关闭</Button>
        </div>
    </Modal>
  </layout>

</template>

<script>
    import {
        Api
    } from "../../../../api/index.js"
    import {sessionStorage} from '@/common/cacheUtils.js';
    export default {
        data() {
            return {
                formCustom: {},
                categoryIds: [],
                tableHeight: 450,
                loading: false,
                form1:{},
                specModels: '',
                styleArr: [{
                    lineHeight: '30px',
                    width: '220px'
                }],
                divStyle: {
                    marginTop: '20px'
                },
                btn: {
                    width: '80px',
                    height: '36px',
                    color: '#fff',
                    borderRadius: '4px',
                    border: '1px solid #dcdee2',
                    textAlign: 'center',
                    verticalAlign: 'middle',
                    marginLeft: '10px',
                },
                pageSizeArray: [5, 10, 20, 50],
                dataCount: 0,
                pageSize: 10,
                currentPage: 1,
                columns1: [     {
                    title: "序号",
                    width: 80,
                    align: "center",
                    fixed:'left',
                    render: (h, params) => {
                        return h('span', params.index + (this.currentPage - 1) * this.pageSize + 1);
                    }
                },
                    {
                        title: '规则型号',
                        align: 'center',
                        key: 'specModels'
                    },
                    {
                        title: '规格型号编码',
                        key: 'specCode',
                        align: 'center'
                    },
                    {
                        title: '设备种类',
                        align: 'center',
                        key: 'categoryName'
                    },
           /*         {
                        title: '所属公司',
                        align: 'center',
                        key: 'belongCompanyName'
                    },      */
                    {
                        title: '备注',
                        align: 'center',
                        key: 'notes'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        fontSize: '14px'
                                    },
                                    on: {
                                        click: () => {
                                            this.showData(params,1);
                                        }
                                    }
                                }, '详情'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                               h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        color: params.row.createUserId == sessionStorage('userId').getObject('userId').userId ||  this.Common.userIdList.includes(sessionStorage('userId').getObject('userId').userId )
                                        ||sessionStorage('userId').getObject('userId').userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined? '':'#d0d0d0' ,
                                        pointerEvents: params.row.createUserId == sessionStorage('userId').getObject('userId').userId ||  this.Common.userIdList.includes(sessionStorage('userId').getObject('userId').userId )
                                        ||sessionStorage('userId').getObject('userId').userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined? '' : 'none'
                                    },
                                    on: {
                                        click: () => {
                                             this.showData(params,2);
                                        }
                                    }
                                }, '修改'),
                                h('Divider', {
                                    props: {
                                        type: 'vertical',
                                    }
                                }, ''),
                                h('A', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        color: params.row.createUserId == sessionStorage('userId').getObject('userId').userId ||  this.Common.userIdList.includes(sessionStorage('userId').getObject('userId').userId )
                                        ||sessionStorage('userId').getObject('userId').userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined? 'red':'#d0d0d0' ,
                                        pointerEvents: params.row.createUserId == sessionStorage('userId').getObject('userId').userId ||  this.Common.userIdList.includes(sessionStorage('userId').getObject('userId').userId )
                                        ||sessionStorage('userId').getObject('userId').userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined? '' : 'none'
                                    },
                                    on: {
                                        click: () => {
                                            this.deleteData(params);
                                        }
                                    }
                                }, '删除'),
                            ]);
                        }
                    }
				],
                showdata: {},
                model1: false,
                buttonLoading: false,model2: false,
                add: false,
                switchFlag: false,
                select: {},
                resultData: [],
                deleteRows: [],
                ruleValidate: {
                    specModels: [
                    { required: true, message: "规格型号名称不能为空", trigger: "change" }
                    ],
                    categoryId: [
                    { required: true, message: "设备种类不能为空", trigger: "change" }
                    ]
                 }
            }
        },
        mounted: function() {
            // this.tableHeight = window.innerHeight * 0.85 - this.$refs.table.$el.offsetTop - 73;
            this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 145;

            //页面加载执行的查询函数,返回的总条数,激活前端分页插件的页码功能.
            this.querySpecModels();
            this.$http.get(Api.queryCategorys).then((res) => {
                this.categoryIds = res.data.data;
            })
        },
        methods: {
            switchQuery(flag){
                this.switchFlag = flag;
                if(!this.switchFlag){
                    this.tableHeight = window.innerHeight - 220;
                }else{
                    this.tableHeight = window.innerHeight - 270;
                }
            },
            handleCancel() {
                this.formCustom = {};
                this.model1 = false;
            },
            addSpecModels() {
                this.model1 = true;
                this.formCustom = {
                    specId: '',
                    specCode: '',
                    specModels: '',
                    categoryId: '',
                    notes: '',
                    belongCompanyId: ''
                };
            },
            modifySpecModel(row) {
                this.model1 = true;
                this.formCustom = row;
            },
            //重置输入框
            reset: function() {
                this.specModels = '';
            },
            //添加
            submit() {
                this.$refs["formCustom"].validate((valid) => {
                    if (valid) { this.buttonLoading=true
                        if (this.formCustom.specId == "") {
                            this.$http.post(Api.specModelsUrl, this.formCustom).then((res) => {
                                if (res.data.status == 200) {
                                    this.querySpecModels();
                                    this.model1 = false;
                                    this.$Message.info('添加成功');
                                } else {
                                    this.$Message.info(res.data.message);
                                } this.buttonLoading=false
                            });
                        } else {
                            this.$http.put(Api.specModelsUrl, this.formCustom).then((res) => {
                                if (res.data.status == 200) {
                                    this.querySpecModels();
                                    this.model1 = false;
                                    this.$Message.info('添加成功');
                                } else {
                                    this.$Message.info(res.data.message);
                                } this.buttonLoading=false
                            });
                        }
                    }
                });
            },
            showData(data, flag) {
                if (flag == 1) {
                    //查看页面
                    this.showdata = data.row;
                    this.model2 = true;
                } else {
                    //修改页面
                    this.formCustom = data.row;
                    this.model1 = true;
                }
            },

            getSelectRows (rows){
                this.deleteRows = rows;
            },
            batDelete(){
                let specId = '';
                this.deleteRows.forEach(item=>{
                    specId = specId  + item.specId + ","
                });
                specId = specId.substring(0, specId.length - 1);

               this.$http.delete(Api.specModelsUrl + '/' + specId).then((res) => {
                        if (res.data.status == 200) {
                            this.$Message.info('删除成功');
                            this.querySpecModels();
                        } else {
                            this.$Message.info('删除失败');
                        }
                    });
            },
            deleteData(params) {
                let istrue = confirm("是否确定删除?");
                if (istrue) {
                    this.$http.delete(Api.specModelsUrl + '/' + params.row.specId).then((res) => {
                        if (res.data.status == 200) {
                            this.$Message.info('删除成功');
                            this.querySpecModels();
                        } else {
                            this.$Message.info('删除失败');
                        }
                    });
                }
            },
            // 查询编码大类列表
            getInfo: function() {this.loading = true ;
                if (this.specModels == undefined) {
                    this.specModels = "";
                }
                this.$http.get(Api.querySpecModel + "?starPage=" + this.currentPage + "&size=" + this.pageSize + "&specModels=" +
                    this.specModels).then((res) => {
                    this.dataCount = res.data.data.totalSize; //总条数
                    this.resultData = res.data.data.data; //查询到的所有编码
                    this.loading = false;
                })
            },

            querySpecModels() {
                this.dataCount = 0;
                this.currentPage = 1;
                this.getInfo();
            },
            changepage(pageIndex) {
                //当页码改变,选择获取当前页数,发送查询请求
                this.currentPage = pageIndex;
                this.getInfo();
            },
            nowPageSize(size) {
                this.pageSize = size;
                this.currentPage = 1;
                this.getInfo();
            },
            rowClassName(row, index) {
                return 'tdHeight';
            }
        }
    }
</script>

<style scoped lang="css">
    @import '../../../../index.css';
</style>
